package com.example.kingdee.kingdeeBridge.kingdeeImpl.impl;

import com.example.kingdee.common.StringUtils;
import com.example.kingdee.exception.ProcedureException;
import com.example.kingdee.kingdeeBridge.common.RequestResult;
import com.example.kingdee.kingdeeBridge.dto.BillWriteInDto;
import com.example.kingdee.kingdeeBridge.entity.SrcBillEntry;
import com.example.kingdee.kingdeeBridge.kingdeeImpl.BillImpl;
import com.example.kingdee.mapper.TBxStMapper;
import com.example.kingdee.sqlTool.SqlTool;
import com.example.kingdee.sqlTool.common.DataObject;
import com.example.kingdee.sqlTool.common.QueryObject;
import com.example.kingdee.sqlTool.sqltoolEnum.FormIdEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.*;

/**
 * 生产用来哦清单变更
 */
@Service
public class PpBOMChangeImpl implements BillImpl {

    @Autowired
    private SqlTool sqlTool;

    @Autowired
    private TBxStMapper tBxStMapper;

    /**
     *查询目标单据
     * @param fid 表id
     * @return
     */
    @Override
    public Map<String, String> selectTargetBillEntry(String fid) {
        return null;
    }
    /**
     *
     * @param fid 表id
     * @param billWriteInDto 源单信息
     * @param targetInfo 目标单据行id和源单行id对应关系
     * @return
     */
    @Override
    public RequestResult updateTargetBillEntry(String fid, BillWriteInDto billWriteInDto, Map<String, String> targetInfo) {
        return null;
    }

    public ArrayList<HashMap<String, Object>> selectTargetBillEntryList(String fid) {
        //查询变更单明细行id
        QueryObject inventorySelect = new QueryObject(FormIdEnum.ppBomChange);
        ArrayList<String> resultKeyArray = new ArrayList<>();
        inventorySelect.addSelect("FEntity_FEntryID");//明细行id
        resultKeyArray.add("entryId");
        inventorySelect.addSelect("FChangeType");//变更类型
        resultKeyArray.add("changeType");
        inventorySelect.addSelect("FMaterialID2.fnumber");//物料编码
        resultKeyArray.add("itemNumber");
        inventorySelect.addCondition("fid", fid);
        return sqlTool.weaveResult(inventorySelect, resultKeyArray);
    }

    public RequestResult updateTargetBillEntryList(String fid, ArrayList<HashMap<String, Object>> entryList) {
        //修改收料数量，仓库
        List<Map<String, Object>> detailEntityList = new ArrayList<>();
        for (HashMap<String, Object> entry : entryList) {
            String changeType = entry.get("changeType").toString();
            //新增的数据
            if (changeType.equals("1")){
                Map<String, Object> itemNumber = new HashMap<>();
                itemNumber.put("fnumber", entry.get("itemNumber"));
                Map<String,Object> detailEntity = new LinkedHashMap<>();
                detailEntity.put("FChangeType", changeType);//变更类型 1——新增， 2——变更前， 3——变更后， 4——取消，5——删除，必填
                detailEntity.put("FMaterialID2", itemNumber);//物料编码
                detailEntity.put("FNumerator", entry.get("numerator"));//分子
                detailEntity.put("FDenominator", entry.get("denominator"));//分母
                detailEntity.put("FPPBOMNo", entry.get("ppBomNumber"));//生产用料清单编号
                detailEntity.put("FMaterialType", entry.get("materialType"));//子项类型
                detailEntity.put("FDosageType", entry.get("dosageType"));//用量类型
                detailEntity.put("FIssueType", entry.get("issueType"));//发料方式
                detailEntity.put("FOverControlMode", entry.get("overControlMode"));//超发控制方式
                detailEntityList.add(detailEntity);
            }
            //删除的数据
            if (changeType.equals("5")){
                Map<String,Object> detailEntity = new LinkedHashMap<>();
                detailEntity.put("FEntryId", entry.get("entryId"));//行id 必填
                detailEntity.put("FNumerator", 0.0);//分子
                detailEntityList.add(detailEntity);
            }
        }
        //修改单据
        DataObject order = new DataObject(FormIdEnum.ppBomChange);
        order.setAttribute("FID", fid);
        order.setIsEntryBatchFill(false);
        order.setIsAutoAdjustField(true);
        order.setAttribute("FENTITY", detailEntityList);//订单明细
        try {
            Map<String, Object> result = sqlTool.editForDataObject(order);
            if (result.get("code").toString().equals("200")){
                return RequestResult.ofSuccess(result);
            }else {
                return RequestResult.ofError(result.get("msg").toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new ProcedureException("无法调用云星空接口");
        }
    }

    public void freshPpBomInfo(ArrayList<HashMap<String, Object>> changeBomList, List<Map<String, Object>> insertList, List<Map<String, Object>> deleteList, String ppBomNumber){
        //处理新增数据
        for (Map<String, Object> insert : insertList) {
            HashMap<String, Object> info = new HashMap<>();
            info.put("changeType", "1");//变更类型
            info.put("itemNumber", insert.get("itemNumber"));//物料编码
            info.put("numerator", insert.get("numerator"));//分子
            info.put("denominator", insert.get("denominator"));//分母
            info.put("ppBomNumber", ppBomNumber);//生产用料清单编号
            info.put("materialType", "1");//子项类型
            info.put("dosageType", "3");//用量类型
            info.put("issueType", "1");//发料方式
            info.put("overControlMode", "1");//超发控制方式
            changeBomList.add(info);
        }
        //处理删除数据
        for (HashMap<String, Object> changeBom : changeBomList) {
            String itemNumber = changeBom.get("itemNumber").toString();
            for (Map<String, Object> delete : deleteList) {
                String deleteItemNumber = delete.get("itemNumber").toString();
                if (deleteItemNumber.equals(itemNumber)){
                    changeBom.put("changeType", "5");
                }
            }
        }
    }

    /**
     *
     * @param billNo 单据编号
     */
    @Override
    public void postPosition(String billNo) {
        //免检的物料直接下推采购入库
    }

    /**
     *
     * @param billNo 单据编号
     */
    @Override
    public void auditTargetBill(String billNo) {
        //提交审核
        sqlTool.auditBill(billNo, FormIdEnum.ppBomChange);
    }

    /**
     * @param fid 单据id
     */
    @Override
    public void deleteTargetBill(String fid) {
        sqlTool.deleteBill(fid, FormIdEnum.stkInStock);
    }


}
